//
//  BirthdayAppendCell.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2018/1/16.
//  Copyright © 2018年 筱鹏. All rights reserved.
//

import UIKit

enum BirthdayAppendCellType {
    case isTextField
    case isLabel
    case isSwitch
}

class BirthdayAppendCell: UITableViewCell {

    var titleLabel: UILabel!
    var textField: UITextField!
    var birthdayLabel: UILabel!
    var alertSwitch: UISwitch!
    var alertClosure: ((Bool)->Void)?
    var textFiledClosure: ((String)->Void)?
    var labelClosure: ((String)->Void)?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = Theme.color.background
        self.contentView.backgroundColor = Theme.color.background
        self.configSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("BirthdayAppendCell init coder")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()

        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func configSubview() {
        let leftBorder: CGFloat = 15
        let rightBorder: CGFloat = 10
        let cellWidth = self.frame.size.width
        titleLabel = UILabel(frame: CGRect(x: leftBorder, y: 10, width: 120, height: 30))
        titleLabel.textColor = Theme.color.title
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        self.contentView.addSubview(titleLabel)
        
        let titleRight = titleLabel.frame.maxX + 10
        let rect = CGRect(x: titleRight, y: 10,
                          width: cellWidth - titleRight - rightBorder,
                          height: 30)
        textField = UITextField(frame: rect)
        textField.font = UIFont.systemFont(ofSize: 18)
        textField.textColor = Theme.color.description
        textField.delegate = self
        textField.layer.borderColor = Theme.color.foreground.cgColor
        textField.layer.borderWidth = 1.0
        textField.keyboardAppearance = .dark
        textField.inputAccessoryView = InputView(title: "完成", { [weak self] in
            self?.textField.resignFirstResponder()
        })
        self.contentView.addSubview(textField)
        textField.autoresizingMask = [.flexibleWidth]
        
        birthdayLabel = UILabel(frame: rect)
        birthdayLabel.font = UIFont.systemFont(ofSize: 16)
        birthdayLabel.textColor = Theme.color.description
        birthdayLabel.layer.borderColor = Theme.color.foreground.cgColor
        birthdayLabel.layer.borderWidth = 1.0
        birthdayLabel.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: .labelTapSel)
        birthdayLabel.addGestureRecognizer(tap)
        self.contentView.addSubview(birthdayLabel)
        birthdayLabel.autoresizingMask = .flexibleWidth
        
        alertSwitch = UISwitch(frame: CGRect(x: cellWidth - 51 - rightBorder, y: 10, width: 51, height: 31))
        alertSwitch.tintColor = Theme.color.foreground
        alertSwitch.onTintColor = Theme.color.foreground
        alertSwitch.addTarget(self, action: .alertSel, for: .valueChanged)
        self.contentView.addSubview(alertSwitch)
        alertSwitch.autoresizingMask = .flexibleLeftMargin
    }
    
    @objc func alertSwitchAction(_ sw: UISwitch) {
        if self.isFirstResponder == false {
            self.becomeFirstResponder()
        }
        guard let closure = self.alertClosure else { return }
        closure(sw.isOn)
    }
    
    @objc func labelTapAction() {
        if self.isFirstResponder == false {
            self.becomeFirstResponder()
        }
        guard let closure = self.labelClosure else { return }
        if let text = birthdayLabel.text {
            closure(text)
        } else {
            closure("")
        }
    }
    
    override var canBecomeFirstResponder: Bool {
        return true
    }
}

private extension Selector {
    static let alertSel = #selector(BirthdayAppendCell.alertSwitchAction(_:))
    static let labelTapSel = #selector(BirthdayAppendCell.labelTapAction)
}

extension BirthdayAppendCell: UITextFieldDelegate {
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        guard let closure = self.textFiledClosure else { return }
        guard let text = textField.text else { return }
        closure(text)
    }
}

extension BirthdayAppendCell {
    
    func config(with title: String, type: BirthdayAppendCellType) {
        if type == .isTextField {
            birthdayLabel.isHidden = true
            alertSwitch.isHidden = true
            textField.isHidden = false
            titleLabel.text = title
        } else if type == .isLabel {
            textField.isHidden = true
            alertSwitch.isHidden = true
            birthdayLabel.isHidden = false
            titleLabel.text = title
        } else if type == .isSwitch {
            textField.isHidden = true
            birthdayLabel.isHidden = true
            alertSwitch.isHidden = false
            titleLabel.text = title
        }
    }
}
